const baseUrl = 'http://localhost:8000'

function ajax({
	method = 'GET',
	url,
	params = {},
	data = {}
} = {}) {
	return new Promise(async function (resolve, reject) {
		let res = await axios({
			method,
			url: baseUrl + url,
			params,
			data,
			headers: {
				Authorization: `Bearer ${localStorage.token}`,
				// Authorization: `Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjYzMjE5N2JkOGMwZmQzMzNmNjQ2ZDE3YyIsImlhdCI6MTY2MzE0OTA2OSwiZXhwIjoxNjYzMzIxODY5fQ.t5_bF8Q835qhLRn9Sjm55830k18CwhLKqY6J6Le0xEg`
			},
		})

		let {
			code,
			msg,
			data: data1
		} = res.data;

		// 验证失败，重新登陆
		if (code == 401) {
			location.href = '/page/login.html';
			return;
		}

		// 服务端错误
		if (code == 500) {
			alert('服务端错误，请稍后重试！')
			return;
		}

		// 其他错误
		if (code) {
			alert(msg)
			return;
		}

		// 请求成功 -> 返回数据

		resolve(data1)
	})
}